package bdz.common.managers {
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.*;
	
	public class ModuleAssetManager extends EventDispatcher
	{
		private var loader:Loader;
		private var context:LoaderContext;
		private var swfURL:String;
		private var definitions:Array;
		private var checkInterval:uint;
		private var loaded:Boolean = false;
		private var nd:ApplicationDomain;
		function ModuleAssetManager(_nd:ApplicationDomain = null):void{
			nd = _nd;
			createLoader();
		}
		private function createLoader():void {
		
			loader = new Loader();
			if(nd == null){
				nd = new ApplicationDomain();
			}
			context = new LoaderContext(false, nd);
			
			loader.contentLoaderInfo.addEventListener(Event.INIT, targetLoaded);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, targetProgress);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, targetIOError);
		}
		private function initialClassCheck():void{
			
			try{
				dispatchEvent(new Event(Event.COMPLETE));
				loaded = true;
			}catch(e:Error){
				dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS));
				setTimeout(initialClassCheck, 100);
			}
			
		}
		private function targetProgress(evt:ProgressEvent):void{
			dispatchEvent(evt);
		}
		private function targetIOError(evt:IOErrorEvent):void{
			dispatchEvent(evt);
		}
		private function targetLoaded(evt:Event):void{
			initialClassCheck();
		}
		
		public function clear():void {
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, targetLoaded);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, targetProgress);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, targetIOError);
			nd = null;
			context = null;
			try{
				loader.close();
			}catch(e:Error){}
			loader.unload();
			//createLoader();
			trace("asset cleared");
		}
		public function set definition(d:Array):void{
			definitions = d;
		}
		public function get definition():Array{
			return definitions;
		}
		public function set url(u:String):void{
			swfURL = u;
			loader.load(new URLRequest(swfURL), context);
		}
		public function get url():String{
			return swfURL;	
		}
		public function get content():*{
			return loader.content;
		}
		public function get loadContext():LoaderContext{
			return context;
		}
		public function get applicationDomain():ApplicationDomain{
			return nd;
		}
		public function classDescription(ci:String):XML{
			var type:XML;
			if(nd.hasDefinition(ci)) {
				type = describeType(nd.getDefinition(ci));
			}
			return type;
		}
		public function getRootVar(ci:String):*{
			return loader.content[ci];
		}
		public function qualifiedClassName(ci:String):String{
			var retString:String;
			if(nd.hasDefinition(ci)) {
				retString = getQualifiedClassName(nd.getDefinition(ci));
			}else{
				retString = null;
			}
			return retString;
		}
		public function retrieveClass(ci:String):*{
			
			var nclass:*;
			if(nd.hasDefinition(ci)) {
				nclass = nd.getDefinition(ci);
			}
			return nclass;
		}
		public function get isLoaded():Boolean{
			return loaded;
		}
	}
}
